<script>
import http from "@/utils/http.js";

const reEmail =
    /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;

function _debounce(fn, rDelay) {
  var delay = rDelay || 600;
  var timer;
  return function () {
    var th = this;
    var args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(function () {
      timer = null;
      fn.apply(th, args);
    }, delay);
  };
}

function toProducts(th) {
  http.get("/malls/products", {mallsid: th.code}).then((res) => {
    let obj = res.data.data
    th.$store.state.contentBars = [];
    let len = Object.keys(obj).length;
    let i = 0;
    for (const objKey in obj) {
      let p = obj[objKey];
      th.$store.state.contentBars[i] = {id: len - 1 - i, code: p.id, name: p.goodsName, info: [p.rest, p.price]};
      i++;
    }
    th.$router.push("/main/products");
  });
}

function toOrders(th) {
  http.get("/home/orderForm", {id: th.$store.state.id}).then((res) => {
    let obj = res.data.data
    th.$store.state.contentBars = [];
    let len = Object.keys(obj).length;
    let i = 0;
    for (const objKey in obj) {
      let p = obj[objKey];
      th.$store.state.contentBars[i] = {id: len - 1 - i, code: p.id, name: p.name, info: [p.date, p.money]};
      i++;
      th.$router.push("/main/orders");
    }
  });
}

function toMain(th) {
  http.get("/home/homePage", {id: th.$store.state.id}).then((res) => {
    let obj = res.data.data;
    th.$store.state.contentBars = [];
    let len = Object.keys(obj).length;
    let i = 0;
    for (const objKey in obj) {
      let p = obj[objKey];
      th.$store.state.contentBars[i] = {id: len - 1 - i, code: p.Id, name: p.Mallname, info: [p.Rest, p.Sales]};
      i++;
    }
    th.$router.push("/main")
  });
}

export default {
  reEmail,
  _debounce,
  toProducts,
  toMain,
  toOrders,
};
</script>
